package com.maple.config;

import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
import com.alibaba.druid.sql.ast.expr.SQLBooleanExpr;
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
import com.alibaba.druid.sql.ast.expr.SQLNumberExpr;
import lombok.Builder;
import lombok.Data;

import java.util.function.Supplier;

/**
 * 字段条件配置
 *
 * @author maple
 * @version 1.0
 * @since 2020-11-27 00:22
 */
@Data
@Builder
public class FieldConditionConfig {
    private String dbType;
    private String fieldName;
    private Class<?> fieldType;
    private SQLBinaryOperator sqlBinaryOperator;
    private Supplier<Object> fieldValueGetter;
    private String tableWhiteListConfigKey;

    public boolean isWhiteTable(String tableName) {
        return true;
    }

    public SQLExpr getValueSqlExpr() {
        if (Number.class.isAssignableFrom(fieldType)) {
            return new SQLNumberExpr((Number) fieldValueGetter.get());
        }

        if (Boolean.class.isAssignableFrom(fieldType)) {
            return new SQLBooleanExpr((Boolean) fieldValueGetter.get());
        }

        return new SQLCharExpr(String.valueOf(fieldValueGetter.get()));
    }
}
